﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Permission.Interfaces;
using Micrhard.FMCG.Server.DataAccess;
using Micrhard.FMCG.Server.Models;

namespace Micrhard.FMCG.Server.Impl
{
    public class DailyCommentDataPermissionValidator : ICustomeDataPermissionValidator
    {
        public bool HasPermission(List<Guid> identity)
        {
            bool result = false;
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var emp = db.Employees.SingleOrDefault(e => e.UserName == this.scope.CurrentUserName);
            //如果当前登录人是业务员
            if (emp.Range == (int)Range.Monitor)
            {
                //当前登录人所管辖的ka
                var mngKa = (from k in db.ManagementScopes
                             where k.Employee.UserName == this.scope.CurrentUserName
                             select k).ToList();
                var daily = db.Dailies.SingleOrDefault(d => identity.Contains(d.DailyID));
                var empid = daily.EmployeeID;
                //促销员工作的ka
                var empKA = (from k in db.ManagementScopes
                             where k.EmployeeID == empid
                             select k.SubKA.SubID).ToList();
                var cnt = mngKa.Count(m => empKA.Contains(m.SubID.Value));
                result = cnt > 0 ? true : false;

            }
            else if (emp.Range == (int)Range.Manager)
            {
                //如果当前登录人是经理
                result = true;
            }
            return result;
        }


        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
